Skip to content

Conversation

@ziggie1984
Copy link
Collaborator

@ziggie1984 ziggie1984 commented Jan 8, 2026

Adds the KV db migration code to native SQL.

After merging this PR, the migration is still hidden behind test_native_sql

@ziggie1984 ziggie1984 self-assigned this Jan 8, 2026
@ziggie1984 ziggie1984 added migration sql payments Related to invoices/payments labels Jan 8, 2026
@gemini-code-assist
Copy link

Summary of Changes

Hello @ziggie1984, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant architectural change by migrating the payments database from a Key-Value (KV) store to a native SQL schema. The primary goal is to enhance database robustness, query flexibility, and overall performance for payment-related operations. It includes the complete implementation of the migration logic, a new SQL-backed payment store, and a comprehensive suite of tests to ensure data integrity and correct functionality during and after the migration. This change also prepares the system for future database optimizations and features by leveraging the capabilities of a relational database.

Highlights

  • KV-to-SQL Migration Logic: Introduced the core migration logic to transfer payment data from the existing Key-Value (KV) store to a new native SQL schema, including handling of historical duplicate payments by converting them into additional HTLC attempts.
  • New SQL Payment Store Implementation: Implemented a new SQL-backed payment store (SQLStore) that adheres to the existing payment database interfaces, providing methods for querying, fetching, initializing, registering, settling, failing, and deleting payments and their HTLC attempts.
  • Comprehensive Testing: Added extensive testing for the migration process, including basic happy-path tests, data integrity checks, property-based tests for various payment scenarios (MPP, AMP, custom records, blinded routes), and a helper for debugging with external database files.
  • Database Schema Integration: Integrated the new payment migration into the main database build process by adding a new paymentMigration constant and a custom migration function in config_builder.go, along with a mechanism to prevent reopening the old KV payments bucket after migration.
  • Payment Data Structures and Utilities: Defined new Go data structures for payments, HTLC attempts, and their statuses (MPPayment, HTLCAttempt, PaymentStatus), along with serialization/deserialization codecs and error types specific to the migration and new SQL store.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@ziggie1984 ziggie1984 added this to v0.21 Jan 8, 2026
@ziggie1984 ziggie1984 added this to the v0.21.0 milestone Jan 8, 2026
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a comprehensive migration for the payments database from a key-value store to SQL. The changes are well-structured, with the migration logic encapsulated in a new payments/db/migration1 package. This package includes a reader for the old KV store format, cleverly handling historical data quirks like duplicate payments by migrating them as HTLC attempts on the primary payment. The migration is correctly wired into the application startup, guarded by a new migration version and a tombstone mechanism to prevent accidental use of the old database post-migration. The testing strategy is particularly strong, featuring data integrity checks, property-based tests, and specific tests for various payment features, which provides high confidence in the correctness of this complex change. My review is positive, with only a couple of minor stylistic suggestions to improve readability.

Comment on lines +232 to +233
return fmt.Errorf("non bucket element" +
"in duplicate bucket")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This string concatenation is unnecessary and can be combined into a single string to improve readability and adhere to the style guide's principle of minimizing lines for error messages.

return fmt.Errorf("non bucket element in duplicate bucket")
References
  1. The style guide states to 'Minimize lines for log and error messages, while adhering to the 80-character limit.' The current code unnecessarily splits a short line, which goes against the 'minimize lines' principle. (link)

@ziggie1984 ziggie1984 force-pushed the elle-payment-sql-series-new branch from d8531d2 to 2930b37 Compare January 8, 2026 12:56
@ziggie1984 ziggie1984 force-pushed the migration-kvdb-sql-payments-part1 branch 2 times, most recently from 2802da4 to 83bce46 Compare January 8, 2026 12:59
@saubyk saubyk moved this to In progress in v0.21 Jan 8, 2026
@ziggie1984 ziggie1984 force-pushed the elle-payment-sql-series-new branch from 2930b37 to 8c106c7 Compare January 8, 2026 15:43
@ziggie1984 ziggie1984 force-pushed the migration-kvdb-sql-payments-part1 branch from 622021b to fc449ad Compare January 10, 2026 23:12
@ziggie1984
Copy link
Collaborator Author

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the migration logic for moving payment data from the key-value store to a SQL database. The changes are extensive, including a new payments/db/migration1 package that encapsulates the migration logic, new SQL schema definitions for duplicate payments, and the necessary wiring in the configuration builder to trigger the migration. The approach is solid, with good isolation of migration code and thorough validation checks to ensure data integrity. I've found a few minor issues related to code style and typos in error messages and comments, which I've detailed in the review comments.

Add a migration specific query which allows to set the failure
reason when inserting a payment into the db.
Older LND versions could create multiple payments for the same hash.
We need to preserve those historical records during KV→SQL migration,
but they don’t fit the normal payment schema because we enforce a
unique payment hash constraint. Introduce a lean payment_duplicates
table to store only the essential fields (identifier, amount,
timestamps, settle/fail data).

This keeps the primary payment records stable and makes the migration
deterministic even when duplicate records lack attempt info. The table
is intentionally minimal and can be dropped after migration if no
duplicate payments exist.

For now there is no logic in place which allows the noderunner to
fetch duplicate payments after the migration.
Copy the core payments/db code into payments/db/migration1 and
add the required sqlc-generated types/queries from sqldb/sqlc.
This effectively freezes the migration code so it stays robust
against future query or schema changes in the main payments package.
@ziggie1984 ziggie1984 force-pushed the migration-kvdb-sql-payments-part1 branch 4 times, most recently from 27f2fec to 5a25e61 Compare January 12, 2026 09:07
@ziggie1984 ziggie1984 marked this pull request as ready for review January 12, 2026 09:09
@ziggie1984 ziggie1984 force-pushed the migration-kvdb-sql-payments-part1 branch 2 times, most recently from 364673d to 5fa0f1f Compare January 12, 2026 09:28
@ziggie1984 ziggie1984 force-pushed the migration-kvdb-sql-payments-part1 branch from 5fa0f1f to 28cb08d Compare January 12, 2026 09:38
Implement the KV→SQL payment migration and add an in-migration
validation pass that deep-compares KV and SQL payment data in batches.
Duplicate payments are migrated into the payment_duplicates table,
and duplicates without attempt info or explicit resolution are marked
failed to ensure terminal state. Validation checks those rows as well.
Add test helpers plus sql_migration_test coverage for KV→SQL migration.
Basic migration, sequence ordering, data integrity, and feature-specific cases
(MPP/AMP, custom records, blinded routes, metadata, failure messages). Also
cover duplicate payment migration to payment_duplicates, including missing
attempt info to ensure terminal failure is recorded.

This gives broad regression coverage for the migration path and its edge-cases.
Add a developer-facing migration_external_test that allows
running the KV→SQL payments migration against a real channel.db
backend to debug migration failures on actual data. The accompanying
testdata README documents how to supply a database file and configure
the test, so users can validate their data and confirm the migration
completes successfully.

The test is skipped by default and meant for manual diagnostics.
Hook the payments KV→SQL migration into the SQL migration config.
The migration is still only available when building with the build tag
"test_native_sql".

Moreover a tombstone protection similar to the invoice migration is added
to prevent re-runningi with the KV backend  once migration completes.
@ziggie1984 ziggie1984 force-pushed the migration-kvdb-sql-payments-part1 branch from 28cb08d to 0360b30 Compare January 12, 2026 09:42
Add a config flag to skip in-migration validation for
the KV->SQL payments migration. This is added as an option
in case bigger payment databases don't require strict
validation but instead prefer speed.

This commit  wires the option through the config, documents
it in the sample config, and disables batch/count validation
when requested.
@ziggie1984 ziggie1984 force-pushed the migration-kvdb-sql-payments-part1 branch from 0360b30 to 915f282 Compare January 12, 2026 09:48
@ziggie1984 ziggie1984 moved this from In progress to In review in v0.21 Jan 12, 2026
@ziggie1984 ziggie1984 added the size/kilo medium, proper context needed, less than 1000 lines label Jan 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

migration payments Related to invoices/payments size/kilo medium, proper context needed, less than 1000 lines sql

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

1 participant